package com.woniu.project.filter;



import com.woniu.project.core.utils.JwtUtil;
import com.woniu.project.properties.FilterProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Klaus
 * @Date: 2020/9/22 12:03
 */
//全局拦截
@Component
public class LoginFilter implements GlobalFilter, Ordered {
    @Autowired
    private FilterProperties filterProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("登录拦截");
        //判断是否是登录页面
        String path = exchange.getRequest().getPath().toString();
        System.out.println("path---------"+path);


        //遍历白名单
        for (String allowPath : filterProperties.getAllowPaths()) {
            System.out.println("allowPath--"+allowPath);
                //判断是否允许
                if(path.startsWith(allowPath)){
                    return chain.filter(exchange);
                }
            }
        if (path.equals("/pawn/management/login")){
            System.out.println("进入登录页面~~~~~~");
            return chain.filter(exchange);
        }
        //如果不是登录页面，判断是否包含token
        //判断是否有token
        String token = exchange.getRequest().getHeaders().getFirst("X-Token");
        System.out.println("LoginFilter中的 X-Token----------"+token);
        try {
            //如果存在则解析 token
            Map<String, Object> map = JwtUtil.parseToken(token);
            for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                String key = stringObjectEntry.getKey();
                if (key.equals("permissionAddrs")){
                    List<String> addrs = (List<String>) stringObjectEntry.getValue();
                    System.out.println("这个人有"+addrs.size()+"个权限");
                    for (String addr : addrs) {
                        if (addr.equals(path)){
                            System.out.println("进入~~~~~~~~~~~");
                            return chain.filter(exchange);
                        }
                    }
                }

            }
            return null;

        }catch (Exception e){
            e.printStackTrace();
            //设置相应头
            exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
            //响应一些数据回去
            return exchange.getResponse().writeWith(Mono.fromSupplier(() -> {
                return exchange.getResponse().bufferFactory().wrap("{\"error\":未登录\"}".getBytes());
            }));
        }
    }

    //返回的Order为过滤链执行的顺序   当存在多个过滤链的时候，order越小则越先执行
    @Override
    public int getOrder() {
        return 1;
    }
}
